import {  listAllFactorys,listPermitFactory } from '@/api/factory'
import {listErpGroup } from "@/api/team/group";
import {workUserInfo} from "@/api/workbench/index";
const common = {
  state: {
    allList:[],
    permitList:[],
    treePermitList:[],
    groupList:[],
    adminDetail:null
  },

  mutations: {
    SET_ALL_LIST: (state, allList) => {
      state.allList = allList
    },
    SET_GROUP_LIST: (state, groupList) => {
      state.groupList = groupList
    },
    PERMIT_LIST: (state, permitList) => {
      state.permitList = permitList
    },
    TREE_PERMIT_LIST: (state, treePermitList) => {
      state.treePermitList = treePermitList
    },
    SET_ADMIN_DETAIL(state, object) {
      state.adminDetail = object;
    }
  },

  actions: {
    //获取所有工厂，车间，产线，工位数据
    getAllData({commit,dispatch }, key) {
      return new Promise((resolve, reject) => {
        listAllFactorys().then(res => {
          if(res.code == 200 && res.data){
            const list = res.data || [];
            commit('SET_ALL_LIST', res.data);
            dispatch("getPermitData")
          }else{
            commit('SET_ALL_LIST', [])
          }
        })
      })
    },

    //获取当前用户有权限的工厂，车间，产线
    getPermitData({ commit,state }) {
      return new Promise((resolve, reject) => {
        listPermitFactory().then(res => {
          if(res.code == 200 && res.data){
            let list = structuredClone(state.allList);
            let permitFactoryList = res.data?.permitFactoryIds || [];
            let permitWorkshopList = res.data?.permitWorkshopIds || [];
            let permitLineList = res.data?.permitLineIds || [];
            let permitStationList = res.data?.permitStationIds || [];
            let permitList = list.filter(ele => permitFactoryList.includes(ele.factoryDeptId));
            if(permitList?.length){
              permitList.forEach(item => {//过滤无权限车间
                if(item?.workshopVos?.length){
                  item.workshopVos = item.workshopVos.filter(obj => permitWorkshopList.includes(obj.workshopId))
                  if(item?.workshopVos?.length){//过滤无权限产线
                    item.workshopVos.forEach(tab => {
                      tab.productionLineVos = tab.productionLineVos.filter(arr => permitLineList.includes(arr.lineId));
                      if(tab?.productionLineVos?.length){//过滤无权限工位
                        tab.productionLineVos.forEach(tar => {
                          tar.workStationVos = tar.workStationVos.filter(goal => permitStationList.includes(goal.workStationId));
                        })
                      }
                    })
                  }
                }
              })
            }
            const treePermitList = permitList.map(factory => {
              return {
                code:factory.factoryDeptCode,
                id:factory.factoryDeptId,
                label:factory.factoryName,
                children:factory?.workshopVos?.length ? factory.workshopVos.map(workshop => {
                  return {
                    code:workshop.workshopCode,
                    id:workshop.workshopId,
                    label:workshop.workshopName,
                    workshopType:workshop.workshopType,
                    children:workshop?.productionLineVos?.length ? workshop.productionLineVos.map(line => {
                      return {
                        code:line.lineCode,
                        id:line.lineId,
                        label:line.lineName,
                        children:line?.workStationVos?.length ? line.workStationVos.map(station => {
                          return {
                            code:station.workStationCode,
                            id:station.workStationId,
                            label:station.workStationName
                          }
                        }):[]
                      }
                    }):[]
                  }
                }):[]
              }
            })
            resolve(res)
            commit('PERMIT_LIST', permitList);
            commit('TREE_PERMIT_LIST', treePermitList);
          }else{
            commit('PERMIT_LIST', [])
          }
        })
      })
    },

    //获取班组信息
    getGroupList({ commit }) {
      const params = {
        pageNum: 1,
        pageSize:900,
        searchValue: ""
      };
      listErpGroup(params)
      .then(res => {
        if (res.code == 200) {
          commit('SET_GROUP_LIST', res?.rows)
        } else {
          commit('SET_GROUP_LIST', [])
        }
      })
    },

    getWorkUserInfo({ commit }){
      return new Promise((resolve, reject) => {
        workUserInfo()
        .then((res) => {
          if(res.code == 200 && res?.data){
            const userInfo = res?.data || {};
            commit('SET_ADMIN_DETAIL', userInfo)
          }
        })
        .catch(() => {
          commit('SET_ADMIN_DETAIL', null)
        });
      })
    }
  }
}
export default common
